package com.dawn.module.infra.controller.app.config;

import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.infra.controller.admin.config.vo.ConfigPageReqVO;
import com.dawn.module.infra.controller.app.config.vo.ConfigRespVO;
import com.dawn.module.infra.dal.dataobject.config.ConfigDO;
import com.dawn.module.infra.service.config.ConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.dawn.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App  - 参数配置")
@RestController
@RequestMapping("/infra/config")
@Validated
@Slf4j
public class AppConfigController {

    @Resource
    private ConfigService configService;

    @GetMapping("/list")
    @Operation(summary = "参数配置集合")
    public CommonResult<List<ConfigRespVO>> list(@RequestParam("category") String category) {
        List<ConfigDO> list = configService.selectListByCategory(category);
        return success(BeanUtils.toBean(list, ConfigRespVO.class));
    }

    @GetMapping("/key")
    @Operation(summary = "参数配置")
    public CommonResult<ConfigRespVO> getConfigByKey(@RequestParam("key") String key) {
        ConfigPageReqVO reqVO = new ConfigPageReqVO();
        reqVO.setKey(key);
        PageResult<ConfigDO> page = configService.getConfigPage(reqVO);
        List<ConfigDO> list = page.getList();
        if(list!=null && list.size()>0){
            return success(BeanUtils.toBean(list.get(0), ConfigRespVO.class));
        }else {
            return CommonResult.success(new ConfigRespVO());
        }
    }
}
